<?php

namespace Admin\Controller;

class AdministratorController extends __Controller {

    protected $form = array(
        'username' => array(
            'title' => '登录名',
            'type' => 'text',
            'name' => 'username'
        ),
        'password' => array(
            'title' => '密码',
            'type' => 'password',
            'name' => 'password'
        ),
        'email' => array(
            'title' => '邮箱',
            'type' => 'email',
            'name' => 'email'
        ),
        'mobile' => array(
            'title' => '手机号',
            'type' => 'tel',
            'name' => 'mobile',
            'maxlength' => 11
        ),
        'group_id' => array(
            'title' => '用户组',
            'type' => 'select',
            'name' => 'group_id'
        )
    );

    public function _initialize() {
        parent::_initialize();

        $groups = M('AdministratorGroup')->getField('id,title');
        $this->form['group_id']['options'] = $groups;
    }

    public function index() {
        $this->title = $this->meta_title = '后台管理员管理';
        $list = $this->lists($this->model,array('id' => array('neq',C('ADMINISTRATOR_ID'))),'id desc');

        if($list) foreach($list as &$item) {
            $item['group_name'] = $this->form['group_id']['options'][$item['group_id']];
        }

        $this->assign('_list_',$list);
        $this->display('index');
    }

    public function add()
    {
        parent::add(); // TODO: Change the autogenerated stub
    }

    public function edit($id) {
        if(!IS_POST)
            $this->model->field('password',true);
        parent::edit($id);
    }

    public function delete($id) {
        if(is_administrator($id))
            $this->error('超级管理员不允许删除。');
        parent::delete($id);
    }

    public function status($id, $value)
    {
        parent::status($id, $value); // TODO: Change the autogenerated stub
    }

}